package com.example.mqtt.thread;

import com.alibaba.fastjson.JSON;
import com.example.mqtt.client.MqttClientConnect;
import com.example.mqtt.entity.EnvStats;
import com.example.mqtt.entity.properties.MQTTProperties;
import com.example.mqtt.service.RowingService;
import com.example.mqtt.tools.SlurryTools;

public class ScheduleEnvStats implements Runnable {

      private String rowingId;

    public ScheduleEnvStats(MQTTProperties mqttProperties, RowingService rowingService) {
        this.mqttProperties = mqttProperties;
        this.rowingService = rowingService;
    }

    public void setRowingId(String rowingId)
    {
        this.rowingId = rowingId;
    }
    private final MQTTProperties mqttProperties;
    private final RowingService rowingService;
    private final MqttClientConnect mqttClientConnect = MqttClientConnect.getInstance();
    @Override
    public void run() {
        try {
            EnvStats environment = new EnvStats();
            environment.setRowingNo(SlurryTools.getRowingNo(rowingId));
            environment.setTimeStamp(System.currentTimeMillis());
            environment.setTemperature(SlurryTools.generateRandomVelocity());
            environment.setHumidity(SlurryTools.generateRandomVelocity());
            mqttClientConnect.sendMessage(SlurryTools.getSplitTopic(mqttProperties.getTrain_env_stats_topic(), rowingService.getRowingById(rowingId).getRowingNo()), JSON.toJSONString(environment));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
}
